﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace TinyServicesControl
{
    public partial class ServicesControl : Form
    {
        private Microsoft.Matrix.WebHost.Server serverMgr;
        private bool m_MgrStarted = false;

        public ServicesControl()
        {
            InitializeComponent();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void refreshServiceList()
        {
            try
            {
                System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("TinyCCServer");
                System.ServiceProcess.ServiceController controller2 = new System.ServiceProcess.ServiceController("TinyVoiceCardServer");

                if (controller != null)
                {
                    ListViewItem item = ServicesListView.FindItemWithText("TinyCCServer");
                    if (item == null)
                    {
                        item = ServicesListView.Items.Add(controller.DisplayName);
                        item.SubItems.Add(controller.Status.ToString());
                        item.SubItems.Add("-1");
                        Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine;
                        Microsoft.Win32.RegistryKey rkSub = rk.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\" + controller.ServiceName);
                        string servicePath = rkSub.GetValue("ImagePath").ToString();
                        item.SubItems.Add(servicePath);
                    }
                    else
                    {
                        item.SubItems[1].Text = controller.Status.ToString();
                    }
                }
                if (controller2 != null)
                {
                    ListViewItem item2 = ServicesListView.FindItemWithText("TinyVoiceCardServer");
                    if (item2 == null)
                    {
                        item2 = ServicesListView.Items.Add(controller2.DisplayName);
                        item2.SubItems.Add(controller2.Status.ToString());
                        item2.SubItems.Add("-1");
                        Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine;
                        Microsoft.Win32.RegistryKey rkSub = rk.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\" + controller2.ServiceName);
                        string servicePath = rkSub.GetValue("ImagePath").ToString();
                        item2.SubItems.Add(servicePath);
                    }
                    else
                    {
                        item2.SubItems[1].Text = controller2.Status.ToString();
                    }
                }
            }
            catch (Exception er)
            {
                this.toolStripStatusLabel1.Text = er.Message;
            }
        }
        private void ServicesControl_Load(object sender, EventArgs e)
        {
            refreshServiceList();
        }

        private void ServicesListView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void tinyMgrToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButtonStart_Click(object sender, EventArgs e)
        {
            startToolStripMenuItem1_Click(sender, e);
        }

        private void startToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                if (ServicesListView.SelectedItems.Count > 0)
                {
                    //process web server
                    if (ServicesListView.SelectedItems[0].Text == "TinyCCMgr")
                    {
                        if (m_MgrStarted == false)
                        {
                            serverMgr.Start();
                            ServicesListView.SelectedItems[0].SubItems[1].Text = "Running";
                            m_MgrStarted = true;
                        }
                        return;
                    }
                    
                    System.ServiceProcess.ServiceController controller =
                        new System.ServiceProcess.ServiceController(ServicesListView.SelectedItems[0].Text);
                    controller.Start();
                    controller.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
                }
                else
                    this.toolStripStatusLabel1.Text = "Please Select the Service!";
            }
            catch (Exception er)
            {
                this.toolStripStatusLabel1.Text = er.Message;
            }
            refreshServiceList();
        }

        private void toolStripButtonStop_Click(object sender, EventArgs e)
        {
            stopToolStripMenuItem1_Click(sender, e);
        }

        private void stopToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                if (ServicesListView.SelectedItems.Count > 0)
                {
                    //process web server
                    if(ServicesListView.SelectedItems[0].Text == "TinyCCMgr")
                    {
                        if (m_MgrStarted == true)
                        {
                            serverMgr.Stop();
                            ServicesListView.SelectedItems[0].SubItems[1].Text = "Stopped";
                            m_MgrStarted = false;
                        }
                        return;
                    }

                    System.ServiceProcess.ServiceController controller =
                        new System.ServiceProcess.ServiceController(ServicesListView.SelectedItems[0].Text);
                    controller.Stop();
                    controller.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
                }
                else
                    this.toolStripStatusLabel1.Text = "Please Select the Service!";
            }
            catch (Exception er)
            {
                this.toolStripStatusLabel1.Text = er.Message;
            }
            refreshServiceList();
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            startToolStripMenuItem1_Click(sender, e);
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stopToolStripMenuItem1_Click(sender, e);
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            refreshServiceList();
        }

        private void toolStripButtonTinyMgr_Click(object sender, EventArgs e)
        {
            if (serverMgr == null)
            {
                System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
                Info.FileName = System.AppDomain.CurrentDomain.BaseDirectory + "\\GacUtil2.exe";
                Info.Arguments = "-i " + System.AppDomain.CurrentDomain.BaseDirectory + "\\Microsoft.Matrix.WebHost.dll";
                Info.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
                Info.WindowStyle = ProcessWindowStyle.Hidden;
                System.Diagnostics.Process Proc;
                try
                {
                    Proc = System.Diagnostics.Process.Start(Info);
                }
                catch (System.ComponentModel.Win32Exception er)
                {
                    this.toolStripStatusLabel1.Text = er.Message;
                    return;
                }   
                try
                {
                    serverMgr = new Microsoft.Matrix.WebHost.Server(81, "/", "C:\\TinyApp\\TinyCC\\Management");
                    ListViewItem item = ServicesListView.FindItemWithText("TinyCCMgr");
                    if (item == null)
                    {
                        item = ServicesListView.Items.Add("TinyCCMgr");
                        item.SubItems.Add("Stopped");
                        item.SubItems.Add("81");
                        item.SubItems.Add("C:\\TinyApp\\TinyCC\\Management");
                        item.SubItems.Add("/");
                    }
                }
                catch (Exception er)
                {
                    this.toolStripStatusLabel1.Text = er.Message;
                }
            }
            else
                this.toolStripStatusLabel1.Text = "WebServer has been Started!";
        }

        private void ServicesControl_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }
    }
}